package cn.alise.controller.gov;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.ListUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.alise.controller.common.BaseController;
import cn.alise.qdp.model.gov.GovAreaInfo;
import cn.alise.qdp.service.gov.GovAreaInfoService;


/**
 * 获取地区数据控制类
 * @author hechj
 * @date 2015-10-08
 * @version 1.0
 *
 */
@Controller
@RequestMapping("/area")
public class AreaController extends BaseController {
	private static final long serialVersionUID = -1835195934242400063L;
	
	/**
	 * 区域服务类
	 */
	@Resource(name = "govAreaInfoService")
	private GovAreaInfoService govAreaInfoService;
	
	/**
	 * 查询身份列表信息
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/queryProvinceList")
	public void queryProvinceList(HttpServletRequest request, HttpServletResponse response) {
		
		List<GovAreaInfo> results = this.govAreaInfoService.getChildren("0");
		if (!CollectionUtils.isEmpty(results)) {
			this.writer(response, results, "查询成功！");
		} else {
			this.writer(response, ListUtils.EMPTY_LIST, "查询成功！");
		}
		
	}
	
	/**
	 * 查询城市列表数据
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/queryCityList")
	public void queryCityList(HttpServletRequest request, HttpServletResponse response, GovAreaInfo area) {
		
		if (!StringUtils.hasLength(area.getAreaCode())) {
			this.writer(response, "1", "省份编号不能为空");
			return;
		}
		
		// 查询城市列表
		List<GovAreaInfo> results = this.govAreaInfoService.getChildren(area.getAreaCode());
		if (!CollectionUtils.isEmpty(results)) {
			this.writer(response, results, "查询成功！");
		} else {
			this.writer(response, ListUtils.EMPTY_LIST, "查询成功！");
		}
	}
	
	/**
	 * 查询地区列表数据
	 * @param request
	 * @param response
	 */
	@RequestMapping(value = "/queryAreaList")
	public void queryAreaList(HttpServletRequest request, HttpServletResponse response, GovAreaInfo area) {
		
		if (!StringUtils.hasLength(area.getAreaCode())) {
			this.writer(response, "1", "城市编号不能为空");
			return;
		}
		
		// 查询地区列表
		List<GovAreaInfo> results = this.govAreaInfoService.getChildren(area.getAreaCode());
		if (!CollectionUtils.isEmpty(results)) {
			this.writer(response, results, "查询成功！");
		} else {
			this.writer(response, ListUtils.EMPTY_LIST, "查询成功！");
		}
	} 
	
	/**
	 * 查询地区详细信息
	 * @param request
	 * @param response
	 * @param area
	 */
	@RequestMapping(value = "/query", method = RequestMethod.POST)
	public void query(HttpServletRequest request, HttpServletResponse response, GovAreaInfo area) {
		
		if (StringUtils.hasText(area.getAreaCode())) {
			
			area = this.govAreaInfoService.getArea(area.getAreaCode());
			if (area != null) {
				this.writer(response, area, "查询成功！");
				return;
			}
		}
		
		this.writer(response, "1", "地区编号为空！");
	}
}
